package com.lagou.assignment.service;

import com.lagou.assignment.dao.StudentDao;
import com.lagou.assignment.factory.StudentDaoFactory;
import com.lagou.assignment.model.Student;

import java.sql.SQLException;
import java.util.List;

public class StudentService {
    private StudentDao studentDao;

    public StudentService() {
        this.studentDao = StudentDaoFactory.getStudentDao();
    }

    /**
     * 自定义成员方法实现根据参数指定的User对象来调用Dao层实现添加学生的功能功能
     * @return int
     */
    public int studentCreateService(Student student) {
        try {
            return studentDao.insert(student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除学生
     * @param id 主键id
     * @return int
     *
     */
    public int studentDeleteService(int id)  {
        try {
            return studentDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 编辑学生信息
     * @param student 学生编辑信息
     * @return int
     *
     */
    public int studentUpdateService(Student student){
        try {
            return studentDao.update(student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<Student> studentFindByNameService(String sname){
        try {
            return studentDao.select(sname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Student studentFindBySidService(int sid) {
        try {
            return studentDao.select("sid", sid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Student studentFindByIdService(int id) {
        try {
            return studentDao.select("id" , id );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Student> studentFindAllService(int page, int limit) {
        try {
            return studentDao.select(page, limit);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int studentFindCountService(){
        try {
            return studentDao.count();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Student studentFindBySidAndNameService(int sid,String sname) {
        Student student = null;
        try {
            student = studentDao.select(sid, sname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return student;
    }
}
